package com.qianfeng.openplatform.geteway.fiter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.qianfeng.openplatform.commons.field.RedisKeyPrefix;
import com.qianfeng.openplatform.commons.field.ResponseMessage;
import com.qianfeng.openplatform.commons.field.StaticField;
import com.qianfeng.openplatform.geteway.client.CacheClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 江江
 * @Date 2020/7/28 15:11
 */
@Component
public class NeedFeeFllter extends ZuulFilter {
    @Autowired
    private CacheClient cacheClient; //redis缓存
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return FilterConstants.PRE_DECORATION_FILTER_ORDER+11;
    }

    @Override
    public boolean shouldFilter() {
        Boolean isFee =true;
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String method = request.getParameter("method");
        try {
            String needfee = cacheClient.hGet(RedisKeyPrefix.WEB_MASTER_API_MAPPING_HASH_PRE + method, "needfee");
            isFee="1".equals(needfee)?true:false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return currentContext.sendZuulResponse()&&isFee;
    }

    @Override
    public Object run() throws ZuulException {
        //进来就是不收费
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String appkey = request.getParameter("appkey");

        try {
            String cusid = cacheClient.hGet(RedisKeyPrefix.WEB_MASTER_API_SERVICE_APPKEY_KEY + appkey, "cusid");
            Long money = cacheClient.hIncrement(RedisKeyPrefix.WEB_MASTER_API_CUSTOMER_KEY + cusid, "money", -2l);
            if (money<0){
                cacheClient.hIncrement(RedisKeyPrefix.WEB_MASTER_API_CUSTOMER_KEY+cusid,"money",2l);
                try {
                    currentContext.setSendZuulResponse(false);//拦截响应
                    ObjectMapper objectMapper = new ObjectMapper();
                    HttpServletResponse response = currentContext.getResponse();
                    response.setContentType("application/json;charset=utf-8");
                    response.getWriter().write(objectMapper.writeValueAsString(new ResponseMessage(StaticField.REQEST_CAST_MONEY_CODE, "屌丝，你的费用不够，请及时充值:")));
                } catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        return null;
    }
}
